﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;

namespace DSPL.Services.Rules
{
    public class CommonValidations : ICommonValidations
    {
        public CommonValidations()
        {
            IUnityContainer container =  GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                ReadService = container.Resolve<IReadService>();
                MasterReadService = container.Resolve<IMasterReadService>();
            }
        }
        public IReadService ReadService { get; set; }
        public IMasterReadService MasterReadService { get; set; }
          
        public  bool ValidateEmptyOrIndexFormControl(List<Control> contList)
        {

            try
            {
                bool state = true;
                foreach (Control cont in contList)
                {
                    switch (cont.GetType().Name)
                    {
                        case "TextBox":
                            if (string.IsNullOrEmpty(cont.Text))
                            {
                                state = false;
                            }
                            break;

                        case "ComboBox":
                            ComboBox cmbx = (ComboBox)cont;
                            if (cmbx.SelectedIndex == -1)
                            {
                                state = false;
                            }break;

                        default:
                            break;
                    }
                }
                return state;
            }
            catch (Exception)
            {
                throw;
            }

        }

        public object ValidateSqlKeywords(object parameter, string dataType)
        {
            if (parameter == null)
                return parameter;
            if (dataType == "TEXT")
            {
                if (parameter.ToString().Contains("'")) //Remove single quote
                    parameter=(object)parameter.ToString().Replace("'", "\"");
                if (parameter.ToString().Contains("''"))//Remove double quote
                    parameter = (object)parameter.ToString().Replace("''", "\"");
                if (parameter.ToString().Contains("--"))//Remove double hypen
                    parameter = (object)parameter.ToString().Replace("--", "-");
                if (parameter.ToString().Contains("|"))//Remove pipe
                    parameter = (object)parameter.ToString().Replace("|", "||");
                if (parameter.ToString().ToUpper().Contains("DELETE "))//Replace DELETE
                    parameter = (object)parameter.ToString().Replace("DELETE", "Cancel");
                if (parameter.ToString().ToUpper().Contains("INSERT "))//Replace INSERT
                    parameter = (object)parameter.ToString().Replace("INSERT", "Input");
                if (parameter.ToString().ToUpper().Contains("SELECT "))//Replace SELECT
                    parameter = (object)parameter.ToString().Replace("SELECT", "Choice");
                if (parameter.ToString().ToUpper().Contains("UPDATE "))//Replace UPDATE
                    parameter = (object)parameter.ToString().Replace("UPDATE", "Replace");
                if (parameter.ToString().ToUpper().Contains("DROP "))//Replace DROP
                    parameter = (object)parameter.ToString().Replace("DROP", "Remove");
                //if (parameter.ToString().ToUpper().Contains("AND "))//Replace AND
                //    parameter = (object)parameter.ToString().ToUpper().Replace("AND", "&");
                //if (parameter.ToString().ToUpper().Contains("OR "))//Replace OR
                //    parameter = (object)parameter.ToString().ToUpper().Replace("OR", "OTHERWISE");

            }

            if (dataType == "DATETIME")
            {
                parameter = Convert.ToDateTime(parameter).ToShortDateString();
            }
            return parameter;
        }

        public bool CheckMasterRecordDuplicates(string tableName, IColumnWithValue[] UniqueKeyList)
        {
            DataTable dtDuplicate= new DataTable();
            if (ReadService != null)
                dtDuplicate = ReadService.GetDuplicates(tableName, UniqueKeyList);
            if (dtDuplicate.Rows.Count > 0)
                return true;
            return false;

        }

        public bool CheckValidyOfDeletion(string tableName, int primaryKey,string primaryFieldName, string ingnoreTableList)
        {
            bool isValid = false;
            if (ReadService != null)
                isValid = (bool)ReadService.CheckValidityOfDeletion(tableName, primaryKey,primaryFieldName, ingnoreTableList);
            return isValid;
        }
        
        public bool UserWiseLocationValidation(object locationCode)
        {
            bool isValid = false;
            if (ReadService != null)
                isValid = (bool)MasterReadService.UserWiseLocationValidity(locationCode, GlobalVariables.UserName);
            return isValid;
        }
    }
}
